<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/9/18 0018
 * Time: 15:30
 */

namespace Admin\Controller;

use Think\Controller;
class FourmController extends Controller
{

    public function index(){
        $id=session('id');
        $fourm=M('fourm')->order('fid desc ')->join('users on users.uid=fourm.uid')->join('news on news.id=fourm.id')->select();
        $reply=M('reply')->order('rid desc')->join('users on users.uid=reply.r_uid')->join('fourm on fourm.fid=reply.r_fid')->select();
       /*dump($reply);
        exit();*/
        for ($i = 0; $i < count($fourm); $i++){
            $fourm[$i]["f_code"] = $i + 1;
        }
        for ($i = 0; $i < count($reply); $i++){
            $reply[$i]["r_code"] = $i + 1;
        }


        if(!$id)
        {$this->error('登录已过期!',U("index.php/Admin/Index/index"));}
        else{
            $this->assign('flist', $fourm);
            $this->assign('rlist', $reply);
            $this->display();
        }

    }

public function fdel(){
    $fid = $_GET['fid'];
    $fourm = M("fourm");
    $fourm->fid = $fid;
    $fourm->delete();
    if ($fid) {
        $this->success('删除评论成功');
    } else {
        $this->error('删除评论失败!');

    }
}


public function rdel(){
    $rid = $_GET['rid'];

    $reply = M("reply");
    $reply->rid = $rid;
    $reply->delete();
    if ($rid) {
        $this->success('删除回复成功');
    } else {
        $this->error('删除回复失败!');

    }
}


    public function fsearch(){
        $ftitle = trim(I('post.ftitle'));
        $newsmodel=M('news');
        $a['title']=array('like',"%{$ftitle}%");
        $fselect=$newsmodel->where($a)->join('fourm on fourm.id=news.id')->join('users on users.uid=fourm.uid')->select();
        for ($i = 0; $i < count($fselect); $i++){
            $fselect[$i]["fs_code"] = $i + 1;
        }
        if($fselect)
        {
            $this->assign('fslist', $fselect);
            $this->display();
        }
        else{ $this->error('未查到相关结果,请重新查询',U("Fourm/index"));}
    }

    public function rsearch(){
        $comment = trim(I('post.comment'));
        $replymodel=M('fourm');
        $a['comment']=array('like',"%{$comment}%");
        $rselect=$replymodel->where($a)->join('reply on reply.r_fid=fourm.fid')->join('users on users.uid=reply.r_uid')->select();
        for ($i = 0; $i < count($rselect); $i++){
            $rselect[$i]["rs_code"] = $i + 1;
        }
        if($rselect)
        {
            $this->assign('rslist', $rselect);
            $this->display();
        }
        else{ $this->error('未查到相关结果,请重新查询',U("Fourm/index"));}
    }




}